package com.cn.jx.configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.jx.fileUtil.txt.TxtUtil;
import com.jx.util.DateUtil;

/**
 * <p>DataBaseInterecptor 拦截器
 * <p>1. 判断系统是否第一次 创建初始化数据库
 * <p>2. 判断系统试用时间，过期拦截
 * 
 * @author <h1>jiangxu2  2019年11月20日</h1>
 * @version v1.11.20.2019
 * @since 1.0
 * 
 * @see #preHandle(HttpServletRequest, HttpServletResponse, Object)
 * 
 */
@Component
public class DataBaseInterecptor extends HandlerInterceptorAdapter{
	
	/**
	  *  读取属性配置文件
	*/
	@Value("${license.day:365}")
	private int day;
	
	/**
	 * 拦截之前执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String path = request.getContextPath();
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int port = request.getServerPort();
        String basePath = scheme + "://" + serverName + ":" + port + path;
		/*
		 * 1. 判断是否第一次安装系统 
		 * 2. 找C:\Program Files\InternetSystem\license.text
		 * 3. 判断今天是否超过install时间 超过则转到到期购买页面
		 */
		String license=TxtUtil.initSystemLicense(day);
		//表示产品过期 
		request.getSession().setAttribute("license", license);//
		
		if(!DateUtil.TodayCompareToDay(license)){
			response.sendRedirect(basePath+"/error/overdue?license="+license);
		}
		 return true;
	}

	
}
